﻿using System;

namespace YArchitech.HVAC
{
	public class DuctInsulationCalc
	{
		public double MediumTemp { get; set; }

		public double DesignTemp { get; set; }

		public double DewingTemp { get; set; }

		public InsulationLayerParam InsulationMaterial { get; set; }

		public int CalHeatingInsulationThickness()
		{
			int result = 0;
			try
			{
				double q = this.CalInsulationQ(this.MediumTemp, this.DesignTemp);
				double sTemp = this.CalInsulationSurfaceTemp(q, this.DesignTemp, true);
				double λ = this.CalConductionλ(this.MediumTemp, sTemp);
				result = this.CalInsulationThickness(λ, q, this.MediumTemp, this.DesignTemp);
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw;
			}
			return result;
		}

		public int CalDewingInsulationThickness()
		{
			int result = 0;
			try
			{
				double q = this.CalInsulationQ(this.MediumTemp, this.DesignTemp);
				double sTemp = this.CalInsulationSurfaceTemp(q, this.DesignTemp, false);
				double λ = this.CalConductionλ(this.MediumTemp, sTemp);
				double kd = this.GetKd();
				result = this.CalInsulationThickness(kd, λ, this.MediumTemp, this.DesignTemp, this.DewingTemp);
			}
			catch (Exception ex)
			{
				ex.ToString();
				throw;
			}
			return result;
		}

		private double CalInsulationQ(double mTemp, double dTemp)
		{
			return (mTemp - dTemp) / 0.81;
		}

		private double CalInsulationSurfaceTemp(double Q, double dTemp, bool beHeat = true)
		{
			double num = 11.63;
			if (!beHeat)
			{
				num = 8.141;
			}
			return Q / num + dTemp;
		}

		private double CalConductionλ(double mTemp, double sTemp)
		{
			double num = (mTemp + sTemp) / 2.0;
			double num2 = Convert.ToDouble(this.InsulationMaterial.CoefficientNum1);
			double num3 = Convert.ToDouble(this.InsulationMaterial.CoefficientNum2);
			return num2 + num3 * num;
		}

		private int CalInsulationThickness(double λ, double Q, double mTemp, double dTemp)
		{
			double thickness = λ * ((mTemp - dTemp) / (0.8 * Q) - 0.085984522785898534);
			return this.GetActualThickness(thickness);
		}

		private int CalInsulationThickness(double Kd, double λ, double mTemp, double dTemp, double dewTemp)
		{
			double thickness = Kd * λ / 8.141 * ((dewTemp - mTemp) / (dTemp - dewTemp));
			return this.GetActualThickness(thickness);
		}

		private double GetKd()
		{
			double result = 1.0;
			string name = this.InsulationMaterial.Name;
			if (name.Equals("聚氨酯泡沫制品") || name.Equals("聚苯乙烯泡沫制品"))
			{
				result = 1.4;
			}
			else if (name.Equals("泡沫玻璃制品"))
			{
				result = 1.1;
			}
			return result;
		}

		private int GetActualThickness(double thickness)
		{
			thickness *= 1000.0;
			double num = thickness / 5.0;
			double num2 = Math.Truncate(num);
			thickness = 5.0 * num2;
			if (num - num2 > 0.0)
			{
				thickness += 5.0;
			}
			return Convert.ToInt32(thickness);
		}

		private const double AS = 11.63;

		private const double AD = 8.141;

		private const double RMin = 0.81;
	}
}
